package org.itboys.ace.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.itboys.admin.constant.AdminConstants;
import org.itboys.admin.dto.AdminUserData;
import org.itboys.admin.dto.CenterMenuDTO;
import org.itboys.admin.entity.BusinessCenterAdminMenu;
import org.itboys.admin.entity.BusinessCenterUser;
import org.itboys.admin.service.BusinessCenterAdminMenuService;
import org.itboys.admin.service.BusinessCenterUserService;
import org.itboys.admin.service.SellerAdminMenuService;
import org.itboys.admin.tools.AdminCookieHolder;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.admin.tools.LoginUser;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.seller.dto.SellerMenuDTO;
import org.itboys.seller.entity.SellerMenu;
import org.itboys.seller.entity.SellerUser;
import org.itboys.seller.meta.InfoPublish;
import org.itboys.seller.service.InfoPublishService;
import org.itboys.seller.service.SellerUserService;
import org.itboys.seller.utils.SellerSessionHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 首页相关
 * 
 * @author 伟哥好强
 * 
 */
@RestController
public class SellerAdminIndexController extends BaseController {

	@Autowired
	private SellerUserService sellerUserService;
	@Autowired
	private ResourceHolder resourceHolder;
    @Resource
    private InfoPublishService infoPublishService;

	@RequestMapping("/getAdminUserData")
	public AdminUserData getAdminUserData() {
		AdminUserData userData = new AdminUserData();
		Long userId = AdminSessionHolder.getAdminUserId();
		if (userId == null) {
			userData.setLogined(false);
			return userData;
		}
		userData = sellerUserService.installAdminUserData(userId);

		return userData;
	}

	/**
	 * 首页跳转
	 * 
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = { "/", "" })
	public ModelAndView index(HttpServletResponse response, Model model)
			throws Exception {

		/*model.addAttribute("staticRoot",resourceHolder.getStringValue("staticRoot"));
		model.addAttribute("adminRoot",resourceHolder.getStringValue("adminRoot"));
		model.addAttribute("imgRoot",resourceHolder.getStringValue("imgRoot"));*/

		if (AdminSessionHolder.getAdminUserId() == null) {
			return new ModelAndView("redirect:/seller/login");
			// response.sendRedirect(resourceHolder.getStringValue("adminLoginUrl"));
		} else {
            String accountId = AdminCookieHolder.getAdminUserName();
			SellerUser sellerUser = sellerUserService.getAdminUser(accountId);
			model.addAttribute("totalAmount",sellerUser.getTotalAmount());
			model.addAttribute("recentTotalAmount",sellerUser.getRecentTotalAmount());
			model.addAttribute("rewardBalance",sellerUser.getRewardBalance());
            model.addAttribute("accountId",accountId);
            model.addAttribute("totalMatch", Math.ceil(infoPublishService.totalInfo(accountId) / 2));
            return new ModelAndView(resourceHolder.getStringValue("adminIndexUrl"));
		}
	}
	@RequestMapping("/sessiondelay")
	public ModelAndView sessionDelay(@RequestParam("type")String type){
		if(type.equals("0")){
			return new ModelAndView("session-delay");
		}else {
			return new ModelAndView("session-delay-index");
		}
	}

}
